<?php


namespace app\manage\model;


use think\Db;

class Statis
{
    /**
     * 数据统计
     */
    public function total(){

    }

    /**
     * 首页数据统计
     */
    public function index(){
        $today_start=strtotime(date('Y-m-d'));//今天凌晨时间戳
        $a=strtotime(date('Y-m-d',strtotime('+1 day')));//明天凌晨时间戳
        $today_end=$a-1;
        $yes_start=strtotime(date('Y-m-d',strtotime('-1 day')));//昨天凌晨时间戳
        $yes_end=$today_start-1;
        $where['create_time'] = [['>= time', $today_start], ['<= time', $today_end]];
        $where2['create_time'] = [['>= time', $yes_start], ['<= time', $yes_end]];

        $where3['is_del']=1;
        $where3['status']=['in',[3,4,5,11,12]];
        $where3['pay_status']=2;
        //今日订单  昨日订单
        $today_order_count=Db::name('order')
            ->where($where3)
            ->where($where)
            ->count();
        $yesterday_order_count=Db::name('order')
            ->where($where3)
            ->where($where2)
            ->count();
        //今日交易额
        $order_score_money=Db::name('order')
            ->where($where3)
            ->where($where)
            ->sum('order_score_money+order_coin_money');
        $today_turnover=sprintf("%.2f",$order_score_money);
        // 昨日交易额
        $order_score_money2=Db::name('order')
            ->where($where3)
            ->where($where2)
            ->sum('order_score_money+order_coin_money');
        $yesterday_turnover=sprintf("%.2f",$order_score_money2);
        //总交易额
        $order_score_money3=Db::name('order')
            ->where($where3)
            ->sum('order_score_money+order_coin_money');
        $total_turnover=sprintf("%.2f",$order_score_money3);

        //昨日新增店铺入驻  店铺入驻总数  昨日新增会员  会员总数
        $yesterday_store_count=Db::name('store_apply')
            ->where(['is_delete'=>1,'status'=>1])
            ->where($where2)
            ->count();
        $total_store_count=Db::name('store_apply')
            ->where(['is_delete'=>1,'status'=>1])
            ->count();
        $yesterday_member_count=Db::name('member')
            ->where(['is_delete'=>1,'is_enable'=>1])
            ->where($where2)
            ->count();
        $total_member_count=Db::name('member')
            ->where(['is_delete'=>1,'is_enable'=>1])
            ->count();

        //待发货条数  评价待回复条数  店铺审核条数
        $delivered_order_count=Db::name('order')
            ->where(['is_del'=>1,'status'=>2,'pay_status'=>2])
            ->count();
        $comment_count=Db::name('comment')
            ->where(['is_reply'=>2])
            ->count();
        $store_examine_count=Db::name('store_apply')
            ->where(['is_delete'=>1,'status'=>2])
            ->count();

        //近7天交易统计，7天总订单量，订单总金额
        $list=$this->statistical();

        //近7天会员统计

        //会员等级比例
        $gold_member=Db::name('member')->alias('m')
            ->join('member_grade mg','mg.member_id=m.member_id')
            ->where(['m.is_delete'=>1,'m.is_enable'=>1,'mg.grade_type'=>3])
            ->where("mg.grade_id in (select max(grade_id) from  mm_member_grade group by member_id)")
            ->count();

        $silver_member=Db::name('member')->alias('m')
            ->join('member_grade mg','mg.member_id=m.member_id')
            ->where(['m.is_delete'=>1,'m.is_enable'=>1,'mg.grade_type'=>2])
            ->where("mg.grade_id in (select max(grade_id) from  mm_member_grade group by member_id)")
            ->count();

        $ordinary_member=Db::name('member')->alias('m')
            ->join('member_grade mg','mg.member_id=m.member_id')
            ->where(['m.is_delete'=>1,'m.is_enable'=>1,'mg.grade_type'=>1])
            ->where("mg.grade_id in (select max(grade_id) from  mm_member_grade group by member_id)")
            ->count();

        $drill_member=Db::name('member')->alias('m')
            ->join('member_grade mg','mg.member_id=m.member_id')
            ->where(['m.is_delete'=>1,'m.is_enable'=>1,'mg.grade_type'=>4])
            ->where("mg.grade_id in (select max(grade_id) from  mm_member_grade group by member_id)")
            ->count();

        $member['drill_member']=$drill_member;
        $member['ordinary_member']=$ordinary_member;
        $member['silver_member']=$silver_member;
        $member['gold_member']=$gold_member;
        $member['total_member']=$total_member_count;




        $data['today_order_count']=$today_order_count;
        $data['yesterday_order_count']=$yesterday_order_count;
        $data['today_turnover']=$today_turnover;
        $data['yesterday_turnover']=$yesterday_turnover;
        $data['total_turnover']=$total_turnover;
        $data['yesterday_store_count']=$yesterday_store_count;
        $data['total_store_count']=$total_store_count;
        $data['yesterday_member_count']=$yesterday_member_count;
        $data['total_member_count']=$total_member_count;
        $data['delivered_order_count']=$delivered_order_count;
        $data['comment_count']=$comment_count;
        $data['store_examine_count']=$store_examine_count;

        $data['member']=$member;


        return $data;
    }


    //统计近7天是销售数据
    public function statistical(){

        $b_day6=mktime(0,0,0,date('m'),date('d')-6,date('Y'));
        $b_day5=mktime(0,0,0,date('m'),date('d')-5,date('Y'));
        $b_day4=mktime(0,0,0,date('m'),date('d')-4,date('Y'));
        $b_day3=mktime(0,0,0,date('m'),date('d')-3,date('Y'));
        $b_day2=mktime(0,0,0,date('m'),date('d')-2,date('Y'));
        $b_day1=mktime(0,0,0,date('m'),date('d')-1,date('Y'));
        $b_day=mktime(0,0,0,date('m'),date('d'),date('Y'));
        $e_day6=$b_day5-1;
        $e_day5=$b_day4-1;
        $e_day4=$b_day3-1;
        $e_day3=$b_day2-1;
        $e_day2=$b_day1-1;
        $e_day1=$b_day-1;
        $e_day=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;

        $order_count7=Db::name('order')
            ->where("create_time >=".$b_day6." and  create_time <=".$e_day6)
            ->where(['is_del'=>1,'status'=>['in',[3,4,5,11,12]],'pay_status'=>2])
            ->count();
        $money7=Db::name('order')
            ->where("create_time >=".$b_day6." and  create_time <=".$e_day6)
            ->where(['is_del'=>1,'status'=>['in',[3,4,5,11,12]],'pay_status'=>2])
            ->sum('order_score_money+order_coin_money');
        $money7=sprintf("%.2f",$money7);

        $order_count6=Db::name('order')
            ->where("create_time >=".$b_day5." and  create_time <=".$e_day5)
            ->where(['is_del'=>1,'status'=>['in',[3,4,5,11,12]],'pay_status'=>2])
            ->count();
        $money6=Db::name('order')
            ->where("create_time >=".$b_day5." and  create_time <=".$e_day5)
            ->where(['is_del'=>1,'status'=>['in',[3,4,5,11,12]],'pay_status'=>2])
            ->sum('order_score_money+order_coin_money');
        $money6=sprintf("%.2f",$money6);

        $order_count5=Db::name('order')
            ->where("create_time >=".$b_day4." and  create_time <=".$e_day4)
            ->where(['is_del'=>1,'status'=>['in',[3,4,5,11,12]],'pay_status'=>2])
            ->count();
        $money5=Db::name('order')
            ->where("create_time >=".$b_day4." and  create_time <=".$e_day4)
            ->where(['is_del'=>1,'status'=>['in',[3,4,5,11,12]],'pay_status'=>2])
            ->sum('order_score_money+order_coin_money');
        $money5=sprintf("%.2f",$money5);

        $order_count4=Db::name('order')
            ->where("create_time >=".$b_day3." and  create_time <=".$e_day3)
            ->where(['is_del'=>1,'status'=>['in',[3,4,5,11,12]],'pay_status'=>2])
            ->count();
        $money4=Db::name('order')
            ->where("create_time >=".$b_day3." and  create_time <=".$e_day3)
            ->where(['is_del'=>1,'status'=>['in',[3,4,5,11,12]],'pay_status'=>2])
            ->sum('order_score_money+order_coin_money');
        $money4=sprintf("%.2f",$money4);

        $order_count3=Db::name('order')
            ->where("create_time >=".$b_day2." and  create_time <=".$e_day2)
            ->where(['is_del'=>1,'status'=>['in',[3,4,5,11,12]],'pay_status'=>2])
            ->count();
        $money3=Db::name('order')
            ->where("create_time >=".$b_day2." and  create_time <=".$e_day2)
            ->where(['is_del'=>1,'status'=>['in',[3,4,5,11,12]],'pay_status'=>2])
            ->sum('order_score_money+order_coin_money');
        $money3=sprintf("%.2f",$money3);

        $order_count2=Db::name('order')
            ->where("create_time >=".$b_day1." and  create_time <=".$e_day1)
            ->where(['is_del'=>1,'status'=>['in',[3,4,5,11,12]],'pay_status'=>2])
            ->count();
        $money2=Db::name('order')
            ->where("create_time >=".$b_day1." and  create_time <=".$e_day1)
            ->where(['is_del'=>1,'status'=>['in',[3,4,5,11,12]],'pay_status'=>2])
            ->sum('order_score_money+order_coin_money');
        $money2=sprintf("%.2f",$money2);

        $order_count1=Db::name('order')
            ->where("create_time >=".$b_day." and  create_time <=".$e_day)
            ->where(['is_del'=>1,'status'=>['in',[3,4,5,11,12]],'pay_status'=>2])
            ->count();
        $money1=Db::name('order')
            ->where("create_time >=".$b_day." and  create_time <=".$e_day)
            ->where(['is_del'=>1,'status'=>['in',[3,4,5,11,12]],'pay_status'=>2])
            ->sum('order_score_money+order_coin_money');
        $money1=sprintf("%.2f",$money1);

        //data1 订单数量  data2 订单销量  data3 日期
        $list=[
            'data1'=>[$order_count7,$order_count6,$order_count5,$order_count4,$order_count3,$order_count2,$order_count1],
            'data2'=>[$money7,$money6,$money5,$money4,$money3,$money2,$money1],
//            'data2'=>[7,$money6,$money5,5,$money3,$money2,1],
            'data3'=>[date("Y.m.d",strtotime("-6 day")),
                date("Y.m.d",strtotime("-5 day")),
                date("Y.m.d",strtotime("-4 day")),
                date("Y.m.d",strtotime("-3 day")),
                date("Y.m.d",strtotime("-2 day")),
                date("Y.m.d",strtotime("-1 day")),
                date("Y.m.d",time())]
        ];

        return $list;
    }

    //统计最近7个月用户数量
    public function member_total(){
        $timestamp = time();
        $start_time = strtotime(date( 'Y-m-1 00:00:00', $timestamp ));
        $mdays = date( 't', $timestamp );
        $end_time = strtotime(date( 'Y-m-' . $mdays . ' 23:59:59', $timestamp ));
        $two_start_time = strtotime(date('Y-m-01 00:00:00',strtotime('-1 month')));
        $two_end_time = strtotime(date("Y-m-d 23:59:59", strtotime(-date('d').'day')));

        $three_start_time = date('Y-m-01 00:00:00',strtotime('-2 month'));
        $three_end_time = strtotime(date("Y-m-d 23:59:59", strtotime("$three_start_time +1 month -1 day")));
        $three_start_time=strtotime($three_start_time);

        $four_start_time = date('Y-m-01 00:00:00',strtotime('-3 month'));
        $four_end_time =strtotime( date("Y-m-d 23:59:59", strtotime("$four_start_time +1 month -1 day")));
        $four_start_time=strtotime($four_start_time);

        $five_start_time = date('Y-m-01 00:00:00',strtotime('-4 month'));
        $five_end_time = strtotime(date("Y-m-d 23:59:59", strtotime("$five_start_time +1 month -1 day")));
        $five_start_time=strtotime($five_start_time);

        $six_start_time = date('Y-m-01 00:00:00',strtotime('-5 month'));
        $six_end_time = strtotime(date("Y-m-d 23:59:59", strtotime("$six_start_time +1 month -1 day")));
        $six_start_time=strtotime($six_start_time);

        $seven_start_time = date('Y-m-01 00:00:00',strtotime('-6 month'));
        $seven_end_time = strtotime(date("Y-m-d 23:59:59", strtotime("$seven_start_time +1 month -1 day")));
        $seven_start_time=strtotime($seven_start_time);

        //$member7=Db::name('member')->where();


    }

}